package com.czx.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.czx.common.constants.CookieKey;
import com.czx.common.constants.RedisKeys;
import com.czx.common.utils.TimeUtils;
import com.czx.entity.Comment;
import com.czx.entity.DiscussPost;
import com.czx.entity.MyPage;
import com.czx.entity.User;
import com.czx.mapper.CommentMapper;
import com.czx.mapper.UserMapper;
import com.czx.service.CommentService;
import com.czx.service.DiscussPostService;
import com.czx.service.PostScoreService;
import com.czx.vo.CommentVo;
import com.czx.vo.UserToken;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.SetOperations;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
@Service
public class CommentServiceImpl extends ServiceImpl<CommentMapper, Comment> implements CommentService {
    @Autowired
    private StringRedisTemplate stringRedisTemplate;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private PostScoreService postScoreService;
    @Autowired
    private DiscussPostServiceImpl discussPostService;

    @Override
    public MyPage disDetailReplay(MyPage page, Integer id, String key, UserToken userToken) {
        SetOperations<String, String> setOperations = stringRedisTemplate.opsForSet();
        //先查评论(评论人)
        MyPage myPage = baseMapper.disDetailReplay(page, id, 1);
        //查询每个帖子下面的回复
        List<CommentVo> records = myPage.getRecords();
        for (CommentVo record : records) {
            List<CommentVo> replyList = baseMapper.disDetailReplay(record.getComment().getId(), 2);
            record.setReplyList(replyList);
            //实现获取点赞数量和点赞
            key = String.format(RedisKeys.USER_LIKE_KEY, CookieKey.COMMENT, record.getComment().getId());
            Boolean member = setOperations.isMember(key, userToken.getUser().getId().toString());
            if (member) {
                record.setLikeStatus(1);
            }else {
                record.setLikeStatus(0);
            }
            //点赞数量
            Long size = stringRedisTemplate.opsForSet().size(key);
            record.setLikeCount(size);
            List<CommentVo> replyList1 = record.getReplyList();
            for (CommentVo commentVo : replyList1) {
                key=String.format(RedisKeys.USER_LIKE_KEY, CookieKey.COMMENT,commentVo.getComment().getId());
                Long size1 = stringRedisTemplate.opsForSet().size(key);
                commentVo.setLikeCount(size1);
                Boolean member1 = setOperations.isMember(key,commentVo.getUser().getId().toString());
                if (member1) {
                    commentVo.setLikeStatus(1);
                }else {
                    commentVo.setLikeStatus(0);
                }
            }
            System.out.println(record.getReplyList());
        }
        return myPage;
    }

    /**
     * 加添评论
     *
     * @param comment
     * @param pid
     * @return
     */
    @Override
    public Integer addComment(Comment comment, Integer pid) {
        comment.setCreateTime(new Date());
        //获取目标的名字
        User user = userMapper.selectById(comment.getTargetId());
        comment.setTargetName(user.getUsername());
        postScoreService.addPostIdToRedis(comment.getEntityId());

        //给帖子评论数量+1
        discussPostService.countByOne(pid);

        return baseMapper.insert(comment);
    }

    /**
     * 通过帖子id获取评论数量
     * @param postId
     * @return
     */
    @Override
    public Long getCommentCountByPostId(Integer postId) {
        Long commentCount = selectCountByEntity(postId);
        return commentCount;
    }

    public Long selectCountByEntity(Integer postId) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("entity_id", postId);
        return baseMapper.selectCount(queryWrapper);
    }
}